//用栈中序遍历二叉树
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
//用递归的方式
/*
class Solution {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null){
            return list;
        }
        inorder(root,list);
        return list;
    }
    public void inorder(TreeNode root, List<Integer> list){
        if(root==null){
            return;
        }
        inorder(root.left,list);
        list.add(root.val);
        inorder(root.right,list);
    }
}

 */
//用迭代的方式
class Solution {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        if (root == null) {
            return list;
        }
        inorder2(root,stack,list);
        return list;
    }
        public void inorder2(TreeNode root,Stack<TreeNode>stack,
                             List<Integer> list){
        if(root==null){
            return;
        }
            stack.push(root);
        if(root.left==null&&root.right==null){
           TreeNode cur=stack.pop();
           list.add(root.val);
           return;
        }
        inorder2(root.left,stack,list);
        list.add((root.val));
        inorder2(root.right,stack,list);
    }
    }